# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: David Macek <david.macek.0@gmail.com>
# Contributor: Felix Huettner <huettner94@gmx.de>

_realname=lua
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.3.5
pkgrel=1
pkgdesc="A powerful light-weight programming language designed for extending applications. (mingw-w64)"
arch=('any')
url="https://www.lua.org/"
license=('MIT')
depends=('winpty')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
options=('staticlibs' 'strip' 'emptydirs')
source=("${url}/ftp/lua-${pkgver}.tar.gz"
        'lua.pc'
        'searchpath.patch'
        'implib.patch'
        'LICENSE')
sha256sums=('0c2eed3f960446e1a3e4b9a1ca2f3ff893b6ce41942cf54d5dd59ab4b3b058ac'
            'ca9252633e782b8f85d6a94ea4f6babd4fe30bd759085b373160b1878e36ff78'
            '3a915911ae680ac3670d6bf96973799baaa7d9c58e6003878eb4369983acc0db'
            'bbb702be90c8915357f8f24b225bdd53d8c7f18d3eb28bd3ed0f39076ab928fa'
            '142fb08b41a807b192b4b2c166696a1830a1c97967e5099ad0e579bf500e1da4')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  cp "${srcdir}/lua.pc" .
  patch -p0 -i "${srcdir}/implib.patch"
  patch -p1 -i "${srcdir}/searchpath.patch"
}

build() {
  [[ -d "${srcdir}/build-${CARCH}" ]] && rm -rf "${srcdir}/build-${CARCH}"
  cp -rf "${srcdir}/${_realname}-${pkgver}" "${srcdir}/build-${CARCH}"
  cd "${srcdir}/build-${CARCH}"
  sed -e "s|%VER%|${pkgver%.*}|g;s|%REL%|${pkgver}|g" \
    -e "s|/usr|${MINGW_PREFIX}|g" \
    -i lua.pc

  make -j1 \
    AR="ar rcu" \
    RANLIB="ranlib" \
    STRIP="strip" \
    CC="${MINGW_CHOST}-gcc" \
    mingw
}

package() {
  cd "${srcdir}/build-${CARCH}"
  make \
    TO_BIN="lua.exe luac.exe lua53.dll" \
    TO_LIB="liblua.a liblua.dll.a" \
    INSTALL_TOP="${pkgdir}${MINGW_PREFIX}" \
    INSTALL_MAN="${pkgdir}${MINGW_PREFIX}"/share/man/man1 \
    install

  # Use winpty to wrap the exe when executed from bash. Please don't move this into a patch as hopefully one day we won't need this hack.
  local _exename="lua"
  local _execname="luac"
  #handle lua.exe and make lua script and symlink equivilant
  mv "${pkgdir}"${MINGW_PREFIX}/bin/${_exename}.exe "${pkgdir}"${MINGW_PREFIX}/bin/${_exename}_exe
  echo "#!/usr/bin/env bash" > "${pkgdir}${MINGW_PREFIX}/bin/${_exename}"
  echo '/usr/bin/winpty "$( dirname ${BASH_SOURCE[0]} )/'${_exename}'.exe" "$@"' >> "${pkgdir}${MINGW_PREFIX}/bin/${_exename}"
  mv "${pkgdir}"${MINGW_PREFIX}/bin/${_exename}_exe "${pkgdir}"${MINGW_PREFIX}/bin/${_exename}.exe
  cp "${pkgdir}${MINGW_PREFIX}/bin/${_exename}" "${pkgdir}${MINGW_PREFIX}/bin/${_exename}${pkgver%.*}"

  #handle luac.exe and make luac script and symlink equivilant
  mv "${pkgdir}"${MINGW_PREFIX}/bin/${_execname}.exe "${pkgdir}"${MINGW_PREFIX}/bin/${_execname}_exe
  echo "#!/usr/bin/env bash" > "${pkgdir}${MINGW_PREFIX}/bin/${_execname}"
  echo '/usr/bin/winpty "$( dirname ${BASH_SOURCE[0]} )/'${_execname}'.exe" "$@"' >> "${pkgdir}${MINGW_PREFIX}/bin/${_execname}"
  mv "${pkgdir}"${MINGW_PREFIX}/bin/${_execname}_exe "${pkgdir}"${MINGW_PREFIX}/bin/${_execname}.exe
  cp "${pkgdir}${MINGW_PREFIX}/bin/${_execname}" "${pkgdir}${MINGW_PREFIX}/bin/${_execname}${pkgver%.*}"

  install -Dm644 lua.pc "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/lua.pc
  install -Dm644 lua.pc "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/lua5.3.pc

  # Install the documentation
  install -d "${pkgdir}${MINGW_PREFIX}"/share/doc/lua
  install -m644 doc/*.{gif,png,css,html} "${pkgdir}${MINGW_PREFIX}"/share/doc/lua
  install -Dm644 ../LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
